/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <baja/common.hpp>
#include <baja/graphics.hpp>
#include <baja/graphics/scenes.hpp>
#include <baja/vars.hpp>
#include <baja/math.hpp>
#include <baja/app/camera.hpp>
#include <baja/app/scene_references.hpp>

namespace baja { namespace app {

namespace g = baja::graphics;
namespace gs = baja::graphics::scenes;
namespace m = baja::math;

class reference_render_object : public g::irender_object
{
public:
	BAJA_DECLARE_TAG("9DC9EBA5-B821-4436-ACCC-024E4787ADC3")

private:
	reference_render_object(const std::shared_ptr<icamera>& camera) : 
		_camera(camera), _showAxes(true), _showFPS(true), _showFloor(true) {}

public:
	friend class std::_Ref_count_obj<reference_render_object>;
	static std::shared_ptr<reference_render_object> create(const std::shared_ptr<icamera>& camera)
	{
		std::shared_ptr<reference_render_object> returnValue = 
			std::make_shared<reference_render_object>(camera);
		return returnValue;
	}

	int32 getPriority() { return 1; }

	void onAddedToRenderService(const std::shared_ptr<g::irender_service>& renderService)
	{
		// add our var group to the var console
		std::shared_ptr<ivar_console> varConsole = services::get<ivar_console>();
		if (varConsole)
		{
			// create var group
			_varGroup = createVarGroup(L"references");
			varConsole->getRoot()->addChildGroup(_varGroup);

			// add var to show/hide references
			_varGroup->addBoolVar(
				L"Show axes",
				&_showAxes,
				0);

			// add var to show/hide FPS
			_varGroup->addBoolVar(
				L"Show FPS",
				&_showFPS,
				0);

			// add var to show/hide floor
			_varGroup->addBoolVar(
				L"Show floor",
				&_showFloor,
				0);
		}
	}
	void onRemovedFromRenderService(const std::shared_ptr<g::irender_service>& renderService)
	{
		// remove our var group from the var console and release it
		std::shared_ptr<ivar_console> varConsole = services::get<ivar_console>();
		if (varConsole)
		{
			varConsole->getRoot()->removeChildGroup(_varGroup);
			_varGroup.reset();
		}
	}

	void render(const std::shared_ptr<g::iengine>& graphics)
	{
		if (_showFPS) 		
			_references->renderFPS();

		if (_showAxes)
			_references->renderAxes();

		if (_showFloor)
			_references->renderFloor();
	}

private:
	bool _showAxes;
	bool _showFloor;
	bool _showFPS;
	std::shared_ptr<ivar_group> _varGroup;
	std::weak_ptr<icamera> _camera;
	std::shared_ptr<scene_references> _references;
};

}}